﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SL8.SL.Helpers;

namespace SL8.SL.Controls
{
    [TemplatePart(Name = CategorySearchItem.PART_CategoryListBox, Type = typeof(ListBox))]
    public class CategorySearchItem : SearchItemBase
    {
        private const string PART_CategoryListBox = "CategoryListBox";

        private ListBox CategoryListBox
        {
            get;
            set;
        }

        public CategorySearchItem()
        {
            this.DefaultStyleKey = typeof(CategorySearchItem);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            this.CategoryListBox = this.GetTemplateChild(CategorySearchItem.PART_CategoryListBox) as ListBox;
        }

        public override System.Linq.Expressions.LambdaExpression GetSearchExpression()
        {
            System.Linq.Expressions.LambdaExpression expression = null;

            if (this.CategoryListBox != null && this.CategoryListBox.ItemsSource != null)
            {
                foreach (var item in this.CategoryListBox.ItemsSource)
                {
                    if (item is SearchColumnInfo)
                    {
                        SearchColumnInfo searchColumnInfo = (SearchColumnInfo)item;

                        if (searchColumnInfo.IsChecked)
                        {
                            System.Linq.Expressions.LambdaExpression expressionTemp = null;

                            if (this.CompareType == CompareType.Equal)
                            {
                                expressionTemp = LinqExpressionHelper.GetEqualExpressionByEntityType(this.ParameterExpression,
                                      searchColumnInfo.ColumnName,
                                      searchColumnInfo.Value,
                                      this.EntityType);
                            }
                            else if (this.CompareType == CompareType.Contains)
                            {
                                expressionTemp = LinqExpressionHelper.GetContainsExpressionByEntityType(this.ParameterExpression,
                                      searchColumnInfo.ColumnName,
                                      searchColumnInfo.Value,
                                      this.EntityType);
                            }

                            if (expressionTemp != null)
                            {
                                if (expression == null)
                                {
                                    expression = expressionTemp;
                                }
                                else
                                {
                                    expression = System.Linq.Expressions.Expression.Lambda(System.Linq.Expressions.Expression.OrElse(expression.Body, expressionTemp.Body), expression.Parameters);
                                }
                            }
                        }
                    }
                }
            }

            return expression;
        }
    }
}
